﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using ByteSite.DBUtility;
using ByteSite.Common;
using SqlSugar;

namespace ByteSite.DAL
{
    /// <summary>
    /// 数据访问类:管理角色表
    /// </summary>
    public partial class manager_role
    {
        private string databaseprefix; //数据库表名前缀
        public SqlSugarClient db;
        public manager_role(string _databaseprefix)
        {
            db = new DBUtility.DbContext().Db;
            databaseprefix = _databaseprefix;
        }

        #region 基本方法================================


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_manager_role model)
        {
            try
            {
                db.BeginTran();

                model.id = db.Insertable<Entitys.dt_manager_role>(model).ExecuteReturnIdentity();
                if (model.manager_role_values != null)
                {
                    foreach (Entitys.dt_manager_role_value modelt in model.manager_role_values)
                    {
                        db.Insertable<Entitys.dt_manager_role_value>(modelt).ExecuteCommand();
                    }
                }

                db.CommitTran();
            }
            catch (Exception ex)
            {
                db.RollbackTran();
                return 0;
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_manager_role model)
        {
            try
            {
                db.BeginTran();

                db.Updateable<Entitys.dt_manager_role>(model).ExecuteCommand();
                db.Deleteable<Entitys.dt_manager_role_value>().Where(x => x.role_id == model.id).ExecuteCommand();
                if (model.manager_role_values != null)
                {
                    foreach (Entitys.dt_manager_role_value modelt in model.manager_role_values)
                    {
                        db.Insertable<Entitys.dt_manager_role_value>(modelt).ExecuteCommand();
                    }
                }

                db.CommitTran();
            }
            catch (Exception ex)
            {
                db.RollbackTran();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            try
            {
                db.BeginTran();
                db.Deleteable<Entitys.dt_manager>().Where(x => x.role_id == id).ExecuteCommand();
                db.Deleteable<Entitys.dt_manager_role_value>().Where(x => x.role_id == id).ExecuteCommand();
                db.Deleteable<Entitys.dt_manager_role>().Where(x => x.id == id).ExecuteCommand();
                db.CommitTran();
            }
            catch (Exception ex)
            {
                db.RollbackTran();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_manager_role GetModel(int id)
        {
            Entitys.dt_manager_role model = db.Queryable<Entitys.dt_manager_role>().Where(x => x.id == id).First();
            model.manager_role_values = db.Queryable<Entitys.dt_manager_role_value>().Where(x => x.role_id == model.id).ToList();
            return model;
        }

        #endregion

    }
}